package com.psp.park.controller;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.psp.park.controller.res.BaseResult;
import com.psp.park.controller.springmvc.req.GetCooperationRecordParam;
import com.psp.park.controller.springmvc.req.GetServiceRecordParam;
import com.psp.park.service.JsjService;

@Component
public class JSJController {
	Logger logger = Logger.getLogger(JSJController.class);

	@Autowired
	JsjService jsjServiceImpl;

	/**
	 * 北斗七星服务需求登记
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult serviceRecord(GetServiceRecordParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			StringBuilder sb = new StringBuilder();
			ServletInputStream input = request.getInputStream();
			byte[] read = new byte[1024];
			int len = -1;
			while ((len = input.read(read)) != -1) {
				sb.append(new String(read, 0, len));
			}
			logger.info("jieshou guolai de shuju :" + sb.toString());
			boolean flag = jsjServiceImpl.serviceRecord(sb.toString());
			logger.info("save gainJSJDate :" + flag);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			logger.info("save gainJSJDate has IOException");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			logger.info("save gainJSJDate has exception");
		}
		return result;
	}

	/**
	 * 制造商、服务商合作记录表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult cooperationRecord(GetCooperationRecordParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			StringBuilder sb = new StringBuilder();
			ServletInputStream input = request.getInputStream();
			byte[] read = new byte[1024];
			int len = -1;
			while ((len = input.read(read)) != -1) {
				sb.append(new String(read, 0, len));
			}
			logger.info("jieshou guolai de shuju :" + sb.toString());
			boolean flag = jsjServiceImpl.cooperationRecord(sb.toString());
			logger.info("save gainJSJDate :" + flag);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			logger.info("save gainJSJDate has IOException");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			logger.info("save gainJSJDate has exception");
		}
		return result;
	}
}
